package com.hanxiaozhang.order.controller;


import com.hanxiaozhang.exception.InternalServerException;
import com.hanxiaozhang.order.entity.User;
import com.hanxiaozhang.order.feign.OldOrderFeignHystrixClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


/**
 * 功能描述: <br>
 * 〈〉
 *
 * @Author:hanxinghua
 * @Date: 2020/6/28
 */
@Slf4j
@RestController
@RefreshScope  // Config Server 配置更改时，得到特殊处理
//@RibbonClient(name = "springcloud-provider-order" ,configuration = RibbonConfig.class) // 配置RibbonClient,在使用Java配置类方式的时候使用
public class OldOrderConsumerController {

  @Autowired
  private RestTemplate restTemplate;

  @Autowired
  private LoadBalancerClient loadBalancerClient;

  @Autowired
  private OldOrderFeignHystrixClient orderFeignHystrixClient;

  @Value("${order.orderServiceUrl:http://springcloud-provider-order/}")
  private String orderServiceUrl;

  @Value("${config.test}")
  private String configTest;

  /**
   * RestTemplate方式使用Ribbon负载均衡测试方法-1
   * 前提：启动两个springcloud-provider-order
   *
   * @param id
   * @return
   */
  @GetMapping("/restTemplate/ribbon/order/{id}")
  public User findByIdByRestTemplateRibbon(@PathVariable Long id) {
    return this.restTemplate.getForObject(orderServiceUrl + id, User.class);
  }

  /**
   * RestTemplate方式使用Ribbon负载均衡测试方法-2
   * 打印当前选择的是哪个节点，验证负载均衡成功
   *
   */
  @GetMapping("/restTemplate/ribbon/log-provider-order-instance")
  public void logUserInstanceByRibbon() {
    ServiceInstance serviceInstance = this.loadBalancerClient.choose("springcloud-provider-order");
    // 打印当前选择的是哪个节点
    OldOrderConsumerController.log.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
  }


//  @Autowired
//  private OldOrderFeignClient orderFeignClient;
//
//  /**
//   * feign方式
//   *
//   * @param id
//   * @return
//   */
//  @GetMapping("/feign/order/{id}")
//  public User findByIdByFeign(@PathVariable Long id) {
//    return this.orderFeignClient.findById(id);
//  }


//  @Autowired
//  private OldOrderFeignClient fileFeignClient;
//
//  /**
//   * feign方式上传文件
//   *
//   * @param file
//   */
//  @PostMapping("/feign/fileUpload")
//  public  void  fileUpload(@RequestParam(value = "file") MultipartFile file){
//    this.fileFeignClient.fileUpload(file);
//  }


  /**
   * hystrix与ribbon结合使用
   *
   * ignoreExceptions作用：发生该类异常时，不会触发回退
   *
   * @param id
   * @return
   */
  @GetMapping("/hystrix/ribbon/order/{id}")
  @HystrixCommand(fallbackMethod = "findByIdFallback",ignoreExceptions = IllegalArgumentException.class)
  public User findByIdByHystrixRibbon(@PathVariable Long id) {
    return this.restTemplate.getForObject(orderServiceUrl+ id, User.class);
  }

  /**
   * 回退方法
   *
   * @param id
   * @param throwable 异常信息
   * @return
   */
  public User findByIdFallback(Long id, Throwable throwable) {
    log.error("findById进入回退方法,异常信息:[{}]",throwable);
    if (throwable instanceof InternalServerException) {
      System.out.println("222");

    }
    User user = new User();
    user.setId(-1L);
    user.setName("默认用户");
    return user;
  }

  @GetMapping("/hystrix/feign/order/{id}")
  public User findByIdByHystrixFeign(@PathVariable Long id) {
    return this.orderFeignHystrixClient.findById(id);
  }


  @GetMapping("/config/test")
  public String configTest() {
    return configTest;
  }

}
